package com.overoid.diary.ui.day.edit;

import com.overoid.diary.R;

import com.overoid.diary.ui.day.edit.DayImage;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class DayEditViewTop02Activity extends Activity{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);	
		
		setContentView(R.layout.day_edit_view_top02);
		
		DayEditActivity act = (DayEditActivity)getParent();
		
		act.top2DayImgsMap.put(0,(ImageButton)findViewById(R.id.dayEditTableImgR01C01));
		act.top2DayImgsMap.put(1,(ImageButton)findViewById(R.id.dayEditTableImgR01C02));
		act.top2DayImgsMap.put(2,(ImageButton)findViewById(R.id.dayEditTableImgR01C03));
		act.top2DayImgsMap.put(3,(ImageButton)findViewById(R.id.dayEditTableImgR01C04));
		act.top2DayImgsMap.put(4,(ImageButton)findViewById(R.id.dayEditTableImgR02C01));
		act.top2DayImgsMap.put(5,(ImageButton)findViewById(R.id.dayEditTableImgR02C02));
		act.top2DayImgsMap.put(6,(ImageButton)findViewById(R.id.dayEditTableImgR02C03));
		act.top2DayImgsMap.put(7,(ImageButton)findViewById(R.id.dayEditTableImgR02C04));
		if(act.memoID !=0){
			Cursor c = act.db.getPictureMediaList(act.memoID);
			
			ImageButton pictureView = null;
			
			c.moveToFirst();
			for(int i=0; i<c.getCount();i++){
				
				pictureView = act.top2DayImgsMap.get(i);
				
				BitmapFactory.Options bitmpaOption = new BitmapFactory.Options();
				Bitmap bitmap = BitmapFactory.decodeFile(c.getString(1), bitmpaOption);
				
				DayImage item = new DayImage(c.getInt(0),c.getString(1),"UPDATE");
				
				pictureView.setTag(item);
				pictureView.setImageBitmap(bitmap);
				
				//act.dayImgs.add(item);
				
				c.moveToNext();
				
			}
			act.dayImgCnt = c.getCount();		
			if (c != null && !c.isClosed()) {
				c.close();
			}		
		}
		
		ImageButton imgR01C01 = (ImageButton)findViewById(R.id.dayEditTableImgR01C01);
		imgR01C01.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				selectPicture((ImageButton)v,0);
				
			}
		});
		
		ImageButton imgR01C02 = (ImageButton)findViewById(R.id.dayEditTableImgR01C02);
		imgR01C02.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				selectPicture((ImageButton)v,1);
			}
		});
		
		ImageButton imgR01C03 = (ImageButton)findViewById(R.id.dayEditTableImgR01C03);
		imgR01C03.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				selectPicture((ImageButton)v,2);
			}
		});
		
		ImageButton imgR01C04 = (ImageButton)findViewById(R.id.dayEditTableImgR01C04);
		imgR01C04.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				selectPicture((ImageButton)v,3);
			}
		});
		
		ImageButton imgR02C01 = (ImageButton)findViewById(R.id.dayEditTableImgR02C01);
		imgR02C01.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				selectPicture((ImageButton)v,4);
			}
		});
		
		ImageButton imgR02C02 = (ImageButton)findViewById(R.id.dayEditTableImgR02C02);
		imgR02C02.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				selectPicture((ImageButton)v,5);
			}
		});
		
		ImageButton imgR02C03 = (ImageButton)findViewById(R.id.dayEditTableImgR02C03);
		imgR02C03.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				selectPicture((ImageButton)v,6);
			}
		});
		
		ImageButton imgR02C04 = (ImageButton)findViewById(R.id.dayEditTableImgR02C04);
		imgR02C04.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				selectPicture((ImageButton)v,7);
			}
		});
	}
	
	private void selectPicture(ImageButton imgBtn,int selectPosition){
		
		DayEditActivity parentAct = (DayEditActivity)getParent();
		LinearLayout preLinear= null;
		LinearLayout currentLinear= null;
		
		if(parentAct.selectImgCnt != selectPosition+1 && imgBtn.getTag()!=null){ //선택된거와 같은것이 아니고 or 빈값이 아닐경우
			
			// 이전 선택된 Picture 부모 LinearLayout 선택해서 테두리 원래대로
			if(parentAct.selectImgCnt !=0){
				preLinear = (LinearLayout)parentAct.top2DayImgsMap.get(parentAct.selectImgCnt-1).getParent();
				preLinear.setBackgroundDrawable(getResources().getDrawable(R.drawable.dayedit_picture_shape));
			}
			
			// 현재 선택된 값 테두리 설정
			currentLinear = (LinearLayout)parentAct.top2DayImgsMap.get(selectPosition).getParent();
			
			
			currentLinear.setBackgroundDrawable(getResources().getDrawable(R.drawable.dayedit_picture_shape_select));
			
			
			parentAct.selectImgCnt =selectPosition+1;
		}
	
	}
}